/*
  Quadruped Robot with ESP32-C3: WIFI
     ssid = "ESP32-C3";
     password = "12345678";
     http://192.168.4.1/
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WebServer.h>
#include "component_gait_servo.h"

// Set these to your desired credentials.
const char *ssid = "ESP32-C3";
const char *password = "12345678";

WebServer server(80);

const char webpage[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
   html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center;}
   .button { background-color: #195B6A; border: none; color: white; padding: 3px 10px;
            width:150px; height: 30px;text-decoration: none; font-size: 18px; margin: 2px; cursor: pointer;}
</style>
</head>
<title>DOG</title>
<body>
<h1 align=center>Quadruped Robot</h1>
<h2 align=center>Trot</h2>
<p><button class="button" onclick="send('F')">Forward</button></p>
<p><button class="button" onclick="send('B')">Backward</button></p>
<p><button class="button" onclick="send('L')">Turn Left</button></p>
<p><button class="button" onclick="send('R')">Turn Right</button></p>
<p><button class="button" onclick="send('Q')">Squatting</button></p>
<h2 align=center>Action</h2>
<p><button class="button" onclick="send('U')">Standing Up</button></p>
<p><button class="button" onclick="send('S')">Sit</button></p>
<p><button class="button" onclick="send('D')">Down</button></p>
<script>
function send(gait_cmd) 
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("state").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "gait_set?state="+gait_cmd, true);
  xhttp.send();
}
</script>
</body>
</html>
)=====";

void webclient_cmd(String gait_cmd)
{
  int cur_cmd =  GAIT_STANDING_UP;
  if (gait_cmd.compareTo("F") == 0)
  {
    cur_cmd =  GAIT_TROT_FORWARD;
  }
  else if (gait_cmd.compareTo("B") == 0)
  {
    cur_cmd =  GAIT_TROT_BACKWARD;
  }
  else if (gait_cmd.compareTo("L") == 0)
  {
    cur_cmd =  GAIT_TROT_LEFT;
  }
  else if (gait_cmd.compareTo("R") == 0)
  {
    cur_cmd =  GAIT_TROT_RIGHT;
  }
  else if (gait_cmd.compareTo("U") == 0)
  {
    cur_cmd = GAIT_STANDING_UP;
  }
  else if (gait_cmd.compareTo("Q") == 0)
  {
    cur_cmd =GAIT_SQUAT;
  }
  else if (gait_cmd.compareTo("S") == 0)
  {
    cur_cmd =GAIT_SIT;
  }
  else if (gait_cmd.compareTo("D") == 0)
  {
    cur_cmd =GAIT_DOWN;
  }
  gait_action(cur_cmd);
}

void handleRoot()
{
  String s = webpage;
  server.send(200, "text/html", s);
}

void handleGait()
{
  String gait_cmd = server.arg("state");
  Serial.println(gait_cmd);
  server.send(200, "text/plane",  gait_cmd);
  webclient_cmd(gait_cmd);
}

void setup_wifi()
{
  Serial.println();
  Serial.println("Configuring access point...");

  // You can remove the password parameter if you want the AP to be open.
  // a valid password must have more than 7 characters
  if (!WiFi.softAP(ssid, password))
  {
    log_e("Soft AP creation failed.");
    while (1)
      ;
  }

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  server.on("/", handleRoot);
  server.on("/gait_set", handleGait);
  server.begin();
  Serial.println("Server started");
}

void loop_wifi()
{
  server.handleClient();
}
